﻿Imports System.Configuration
Imports NewHeaderSystems.Archive.Data

Public Class ConfigurationHelper

    Dim _config As Configuration
    Dim _connectionStringSettings As ConnectionStringSettings

    Public Sub New()
        _config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None)
        _connectionStringSettings = _config.ConnectionStrings.ConnectionStrings("NewHeaderSystems.Archive.Data.My.MySettings.NHSArchiveConnectionString")
    End Sub

    Public Function GetDatabaseFilePath() As String
        Dim databaseFilePath As String = String.Empty
        Dim server As String = String.Empty
        Dim database As String = String.Empty
        GetDatabaseSettings(databaseFilePath, server, database)
        Return databaseFilePath
    End Function

    Public Sub GetDatabaseSettings(ByRef databaseFilePath As String, ByRef server As String, ByRef database As String)
        databaseFilePath = String.Empty
        server = String.Empty
        database = String.Empty

        Dim connectionParts() As String = _connectionStringSettings.ConnectionString.Split(CChar(";"))

        For Each part In connectionParts
            If part.StartsWith("AttachDbFilename", StringComparison.OrdinalIgnoreCase) Then
                databaseFilePath = part.Split(CChar("="))(1)
            ElseIf part.StartsWith("Server", StringComparison.OrdinalIgnoreCase) Then
                server = part.Split(CChar("="))(1)
            ElseIf part.StartsWith("Database", StringComparison.OrdinalIgnoreCase) Then
                database = part.Split(CChar("="))(1)
            End If
        Next
    End Sub

    Public Sub SaveConnectionStringFile(ByVal path As String)
        _connectionStringSettings.ConnectionString = GetNewConnectionString(path)
        _config.Save(ConfigurationSaveMode.Modified)
        DataScript.ConnectionString = Nothing 'Force the new connection string to be used.
    End Sub

    Public Sub SaveConnectionStringServer(ByVal server As String, ByVal database As String)
        _connectionStringSettings.ConnectionString = GetConnectionStringServer(server, database)
        _config.Save(ConfigurationSaveMode.Modified)
        DataScript.ConnectionString = Nothing 'Force the new connection string to be used.
    End Sub

    Public Function TestConnection() As Boolean
        Dim errorMessage As String = String.Empty
        Return DataScript.IsValidConnection(_connectionStringSettings.ConnectionString, errorMessage)
    End Function

    Public Function TestConnectionFile(ByVal path As String, ByRef errorMessage As String) As Boolean
        Return DataScript.IsValidConnection(GetNewConnectionString(path), errorMessage)
    End Function

    Public Function TestConnectionServer(ByVal server As String, ByVal database As String, ByRef errorMessage As String) As Boolean
        Return DataScript.IsValidConnection(GetConnectionStringServer(server, database), errorMessage)
    End Function

    Private Function GetNewConnectionString(ByVal databaseFilePath As String) As String
        Return DataScript.GetConnectionString(databaseFilePath)
    End Function

    Private Function GetConnectionStringServer(ByVal server As String, ByVal database As String) As String
        Return DataScript.GetConnectionString(server, database)
    End Function
End Class
